//
//  HMStatusOriginalView.swift
//  sinaweibo
//
//  Created by WGP on 16/4/14.
//  Copyright © 2016年 WGP. All rights reserved.
//

import UIKit

class HMStatusOriginalView: UIView {
    
//    微博模型
    var status: HMStatus?
    {
        didSet
        {
//            当cell吧模型传给view后,view就根据模型来设置内容
            if let profile_image_url = status?.user?.profile_image_url
            {
                let url = NSURL(string: profile_image_url)!
//                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
                
                iconView.sd_setImageWithURL(url, completed: { (image, error, _, _) -> Void in
                    
                    if error != nil
                    {
                        return
                    }
                    
                    image.GP_AsynDrawImage(nil, drawFinished: {
                        (image) -> () in
                        self.iconView.image = image;
                    })
                })
            }
            
            nameLabel.text = status?.user?.screen_name
            
//            将时间转换成规定的格式
            timeLabel.text = NSDate.date(status?.created_at ?? "刚刚").sinaDateDescription()
            
            sourceLabel.text = status?.source
            
            textLabel.attributedText = status?.attrText
            
//            vipImageView.image = status?.user?.mbrankImage
//         先判断有没有会员等级,没有的话就付为nil否则会发生复用的错误
            if let mbrankImage = status?.user?.mbrankImage
            {
               mbrankImage.GP_AsynDrawImage(nil, drawFinished: {
                    (image) -> () in
                    self.vipImageView.image = image
                })
            }
            
            verifiedImageView.image = status?.user?.verifiedImage
            
            pictureView.pic_urlsArr = status?.pic_urlsArr
            
//            当没有图片的时候,配图View距离textLabel的地步不需要8的距离
            
            pictureView.snp_updateConstraints { (make) -> Void in
                let margin = status?.pic_urlsArr == nil ? 0 : HMStatusCellMargin
                
                make.top.equalTo(textLabel.snp_bottom).offset(margin)
            }
            
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()
        setupSubviews()
    }

//    MARK: - 懒加载
    private func setupSubviews()
    {
//        1.添加子空间
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(vipImageView)
        addSubview(verifiedImageView)
        addSubview(textLabel)
        addSubview(pictureView)
        
//        2.添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.left.top.equalTo(self).offset(HMStatusCellMargin)
            make.width.height.equalTo(35)
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(HMStatusCellMargin)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(iconView)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(HMStatusCellMargin)
            make.centerY.equalTo(timeLabel)
        }
        
        vipImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel)
            make.left.equalTo(nameLabel.snp_right).offset(HMStatusCellMargin)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        textLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(HMStatusCellMargin)
            make.left.equalTo(iconView)
            make.right.equalTo(self).offset(-HMStatusCellMargin)
        }
        
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_right)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(textLabel.snp_bottom).offset(HMStatusCellMargin)
            make.left.equalTo(self).offset(HMStatusCellMargin)
            make.size.equalTo(CGSizeZero)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(pictureView).offset(HMStatusCellMargin)

        }
    }
    
//    MARK: - 懒加载
//    头像
    private lazy var iconView: UIImageView =
    {
        let imageView = UIImageView(imageName: "avatar")
        
//        imageView.layer.cornerRadius = HMIconViewWidth
//        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
//    昵称label
    private lazy var nameLabel: UILabel = UILabel(fontSize: HMNameLabelFontSize, textColor: UIColor.darkGrayColor())

    
//    时间label
    private lazy var timeLabel: UILabel = UILabel(fontSize: HMTimeLabelFontSize, textColor: UIColor.orangeColor())

    
//    来源label
    private lazy var sourceLabel: UILabel = UILabel(fontSize: HMTimeLabelFontSize, textColor: UIColor.lightGrayColor())

    
//    会员imageView
    private lazy var vipImageView: UIImageView = UIImageView(imageName: "common_icon_membership_expired")
    
//    认证图标
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
//    微博内容
    private lazy var textLabel: FFLabel  =
    {
        let label = FFLabel(fontSize: HMTextLabelFontSize, textColor: UIColor.darkGrayColor())
        
        let result = arc4random_uniform(100) % 2
        label.text = result == 0 ? "要下大雨了:要下大雨了:要下大雨了:要下大雨了:要下大雨了:要下大雨了:要下大雨了:要下大雨了:要下大雨了:要下大雨了:" : "明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨明天不要下雨"
        label.sizeToFit()
        label.numberOfLines = 0
        
        return label
    }()
    

/// 原创微博配图视图
    
    private lazy var pictureView: HMStatusPictureView = HMStatusPictureView()
}


















